/*
function internal_debug ($) {
    var panel = $('<div class="inner-debugger"/>').appendTo ($(document.body));
    var style = $('<style/>').appendTo ($(document.head));
    var pretty = function (value) { return value < 10 ? '0' + value : value; }
    var timestamp = function () {
        var date = new Date (), year = date.getFullYear (), month = date.getMonth () + 1, day = date.getDate (),
            hour = date.getHours (), minute = date.getMinutes (), second = date.getSeconds (), s = '';
        s += year + '-' + pretty (month) + '-' + pretty (day) + ' ';
        s += pretty (hour) + ':' + pretty (minute) + ':' + pretty (second);
        return s;
    }
    style.text ('.inner-debugger {position:fixed; width: 50%; height: 300px; right:0; top:0; opacity:.75; background-color:#2b2b2b; color: #999; padding: 2px 4px; overflow:auto}');
    window.d = function (msg) {
        var div = $('<div/>').appendTo (panel);
        div.append ('[').append ($('<span/>').text (timestamp ())).append ('] ');
        div.append (msg);
    }
};
*/

// jfx bridge: js call jfx
if (!window.jfx) {
    var jfx = {
        require : function (action, params) {}
    }
}

if (!window.d) {
    window.d = function (msg) {
        console.log (msg);
    };
}

// local bridge: 
var bridge = (function () {
    function changeConsole () {
        // internal_debug (jQuery);
        // window.d = function () {}

        console.log = function (message) {
            jfx.log (message);
        };
        console.error = function (message) {
            jfx.error (message);
        }
/*
        jfx.log ('server setup complete.');
        jfx.log ('bridge.onload = ' + bridge.onload);
        jfx.log ('typeof bridge.onload = ' + (typeof bridge.onload));
        
        if ('onload' in bridge && typeof bridge.onload === 'function') {
            setTimeout (function () {
                bridge.onload ();
            }, 0);
        }
*/
    }

    function delegate (name, args) {
        var temp = window [name], type = (typeof temp);
        // console.log ('......................... [' + type + '] ' + temp + '..........................');
        if ((name in window) && (typeof window[name] === 'function')) {
            console.log ('delegating function: ' + name + ' with args: ' + args);

            return window[name].apply (window, args);
        } else {
            console.log ('the delegated function: ' + name + ' not found. ignore it!');
        }
    }

    return {
        confirm_close : function () {
            console.log ('confirming page close ...');
            var ret = delegate ('on_close', arguments);
            return ret === undefined || ret === null ? true : ret;
        },
        init_page : function () {
            // at first, bridge the console.log to jfx.log
            changeConsole ();
            // now, the console.log will show the log in jfx

            // updating web-ui use set locale
            delegate ('update_ui');
        },
        fire_page_load : function () {
            // console.log ('fire page load...');
            delegate ('on_page_load', arguments);
        },
        require : function (action, params) {
            var o;
            params = (params === undefined || params === null) ? '' : params;
            try {
                o = jfx.require (action, params);
            } catch (ex) {
                console.error (ex);
                throw ex;
            }
            o = eval ('(' + o + ')');
            if (o.code !== 0) {
                throw o.data;
            }
            return o.data;
        }
    };
})();

$(function () {
    // disable all text selection
    $(window).on ('selectstart', function () {return false;});
});